// const { think } = require('thinkjs');
const { default: BigNumber } = require('bignumber.js');
const moment = require('moment');
const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;
    const type = this.get('type');

    const query = {
      user_id: this.getLoginUserId()
    };

    if (this.get('key')) {
      query['d.name|a.order_no'] = ['like', `%${this.get('key')}%`];
    }

    if (think.isEmpty(type)) {
      return this.fail(408);
    }
    if (type !== '999') {
      query['a.status'] = type;
      if (type === '0') {
        query['a.status'] = ['IN', '0, 10'];
      } else if (type === '30') {
        query['a.status'] = ['IN', '30, 31'];
      }
    }

    const orders = await this.model('order_list').alias('a').join({
      product_list: {
        join: 'left',
        as: 'd',
        on: ['product_id', 'id']
      },
      user_list: {
        join: 'left',
        as: 'u',
        on: ['user_id', 'id']
      }
    }).field('d.name, d.price, d.pic, u.name as username, a.*').order(['id DESC']).where(query).page(page, size).countSelect();
    return this.success(orders);
  }
  async updateContractAction() {
    const id = this.post('id');
    const url = this.post('url');
    await this.model('order_list').where({id: id}).update({
      contract_back: url
    });
    return this.success();
  }

  async updateRefundAction() {
    const id = this.post('id');
    await this.model('order_list').where({id: id}).update({
      status: 60
    });
    return this.success();
  }

  async updateConfirmAction() {
    const id = this.post('id');
    await this.model('order_list').where({id: id}).update({
      status: 40,
      confirm_time: ['exp', 'CURRENT_TIMESTAMP()']
    });
    const order = await this.model('order_list').where({id: id}).find();
    const rate = await this.model('set').where({id: 7}).find();
    const company = await this.model('company_list').where({id: order.company_id}).find();
    const price = BigNumber(order.price).plus(BigNumber(order.price_back));
    const p = price.multipliedBy(BigNumber(1).minus(BigNumber(rate.content)));
    const total = BigNumber(company.balance).plus(p).toNumber();
    await this.model('company_list').where({id: order.company_id}).update({
      balance: total
    });
    await this.model('company_logs').add({
      money: price,
      company_id: order.company_id,
      order: order.id,
      type: 1,
      create_time: ['exp', 'CURRENT_TIMESTAMP()'],
      status: 1,
      update_time: ['exp', 'CURRENT_TIMESTAMP()']
    });
    return this.success();
  }

  async detailAction() {
    const id = this.get('id');
    const data = await this.model('order_list').alias('a').join({
      product_list: {
        join: 'left',
        as: 'd',
        on: ['product_id', 'id']
      }
    }).field('d.name, d.price, d.pic, a.*').where({'a.id': id}).find();
    return this.success(data);
  }

  async addCommentAction() {
    const id = this.post('id');
    const score = this.post('score');
    const remark = this.post('remark');
    const order = await this.model('order_list').where({id: id}).find();
    await this.model('order_comment').add({
      order_id: id,
      score: score,
      remark: remark,
      product_id: order.product_id,
      create_time: ['exp', 'CURRENT_TIMESTAMP()'],
      user_id: this.getLoginUserId()
    });
    await this.model('order_list').where({id: id}).update({
      status: 50
    });
    return this.success();
  }
};
